import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.StringTokenizer;

/**
 * @author AMPM
 */

public class BobClientHandler implements Runnable {
	private Socket socket;
	private static final String privateKey = "bbbbbbbb";
	private String sharedKey;

	public BobClientHandler(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {

			// get the input and output stream from the socket
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();

			// wrap them in the ObjectInputStream and ObjectOutputStream objects
			ObjectOutputStream oos = new ObjectOutputStream(os);
			ObjectInputStream ois = new ObjectInputStream(is);

			// long nonce = (Long) ois.readObject();
			String ticket = (String) ois.readObject();
			String ticketDecrypted = SymmetricCrypt.DecryptString(ticket,
					privateKey);
			StringTokenizer st = new StringTokenizer(ticketDecrypted);
			String sender = st.nextToken();
			System.out.println(sender);
			sharedKey = st.nextToken();
			System.out.println(sharedKey);

			String secretMessageToAlice = SymmetricCrypt.EncryptString(
					"I find you very attractive!", sharedKey);
			oos.writeObject(secretMessageToAlice);

			oos.flush();

		} catch (IOException ignore) {
		} catch (ClassNotFoundException ignore) {
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
